1 /*
2  * Copyright (c) 2011-2012 - Mauro Carvalho Chehab
3  * Copyright (c) 2012 - Andre Roth <neolynx@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation version 2.1 of the License.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  * Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
18  *
19  */
20 
21 /**
22  * @file pmt.h
23  * @ingroup dvb_table
24  * @brief Provides the descriptors for PMT MPEG-TS table
25  * @copyright GNU Lesser General Public License version 2.1 (LGPLv2.1)
26  * @author Mauro Carvalho Chehab
27  * @author Andre Roth
28  *
29  * @par Relevant specs
30  * The table described herein is defined at:
31  * - ISO/IEC 13818-1
32  *
33  * @see http://www.etherguidesystems.com/help/sdos/mpeg/syntax/tablesections/pmts.aspx
34  *
35  * @par Bug Report
36  * Please submit bug reports and patches to linux-media@vger.kernel.org
37  */
38 
39 module libdvbv5_d.pmt;
40 
41 import core.sys.posix.unistd;
42 
43 import libdvbv5_d.descriptors: dvb_desc;
44 import libdvbv5_d.dvb_fe: dvb_v5_fe_parms;
45 import libdvbv5_d.header: dvb_table_header;
46 
47 extern (C):
48 
49 /* ssize_t */
50 
51 /**
52  * @def DVB_TABLE_PMT
53  *	@brief PMT table ID
54  *	@ingroup dvb_table
55  */
56 enum DVB_TABLE_PMT = 0x02;
57 
58 /**
59  * @enum dvb_streams
60  * @brief Add support for MPEG-TS Stream types
61  * @ingroup dvb_table
62  *
63  * @var stream_reserved0
64  *	@brief	ITU-T | ISO/IEC Reserved
65  * @var stream_video
66  *	@brief	ISO/IEC 11172 Video
67  * @var stream_video_h262
68  *	@brief	ITU-T Rec. H.262 | ISO/IEC 13818-2 Video or ISO/IEC 11172-2 constrained parameter video stream
69  * @var stream_audio
70  *	@brief	ISO/IEC 11172 Audio
71  * @var stream_audio_13818_3
72  *	@brief	ISO/IEC 13818-3 Audio
73  * @var stream_private_sections
74  *	@brief	ITU-T Rec. H.222.0 | ISO/IEC 13818-1 private_sections
75  * @var stream_private_data
76  *	@brief	ITU-T Rec. H.222.0 | ISO/IEC 13818-1 PES packets containing private data
77  * @var stream_mheg
78  *	@brief	ISO/IEC 13522 MHEG
79  * @var stream_h222
80  *	@brief	ITU-T Rec. H.222.0 | ISO/IEC 13818-1 Annex A DSM-CC
81  * @var stream_h222_1
82  *	@brief	ITU-T Rec. H.222.1
83  * @var stream_13818_6_A
84  *	@brief	ISO/IEC 13818-6 type A
85  * @var stream_13818_6_B
86  *	@brief	ISO/IEC 13818-6 type B
87  * @var stream_13818_6_C
88  *	@brief	ISO/IEC 13818-6 type C
89  * @var stream_13818_6_D
90  *	@brief	ISO/IEC 13818-6 type D
91  * @var stream_h222_aux
92  *	@brief	ITU-T Rec. H.222.0 | ISO/IEC 13818-1 auxiliary
93  * @var stream_audio_adts
94  *	@brief	ISO/IEC 13818-7 Audio with ADTS transport syntax
95  * @var stream_video_14496_2
96  *	@brief	ISO/IEC 14496-2 Visual
97  * @var stream_audio_latm
98  *	@brief	ISO/IEC 14496-3 Audio with the LATM transport syntax as defined in ISO/IEC 14496-3 / AMD 1
99  * @var stream_14496_1_pes
100  *	@brief	ISO/IEC 14496-1 SL-packetized stream or FlexMux stream carried in PES packets
101  * @var stream_14496_1_iso
102  *	@brief	ISO/IEC 14496-1 SL-packetized stream or FlexMux stream carried in ISO/IEC14496_sections.
103  * @var stream_download
104  *	@brief	ISO/IEC 13818-6 Synchronized Download Protocol
105  * @var stream_reserved
106  *	@brief	ITU-T Rec. H.222.0 | ISO/IEC 13818-1 Reserved (from 0x15 to 0x7f)
107  * @var stream_private
108  *	@brief	User Private (from 0x80 to 0xff)
109  */
110 enum dvb_streams
111 {
112     stream_video = 0x01,
113     stream_video_h262 = 0x02,
114     stream_audio = 0x03,
115     stream_audio_13818_3 = 0x04,
116     stream_private_sections = 0x05,
117     stream_private_data = 0x06,
118     stream_mheg = 0x07,
119     stream_h222 = 0x08,
120     stream_h222_1 = 0x09,
121     stream_13818_6_A = 0x0A,
122     stream_13818_6_B = 0x0B,
123     stream_13818_6_C = 0x0C,
124     stream_13818_6_D = 0x0D,
125     stream_h222_aux = 0x0E,
126     stream_audio_adts = 0x0F,
127     stream_video_14496_2 = 0x10,
128     stream_audio_latm = 0x11,
129     stream_14496_1_pes = 0x12,
130     stream_14496_1_iso = 0x13,
131     stream_download = 0x14,
132     stream_video_h264 = 0x1b,
133     stream_audio_14496_3 = 0x1c,
134     stream_video_hevc = 0x24,
135     stream_video_cavs = 0x42,
136     stream_video_moto = 0x80,
137     stream_audio_a52 = 0x81,
138     stream_scte_27 = 0x82,
139     stream_audio_sdds = 0x84,
140     stream_audio_dts_hdmv = 0x85,
141     stream_audio_e_ac3 = 0x87,
142     stream_audio_dts = 0x8a,
143     stream_audio_a52_vls = 0x91,
144     stream_spu_vls = 0x92,
145     stream_audio_sdds2 = 0x94
146 }
147 
148 /**
149  * @brief Converts from enum dvb_streams into a string
150  * @ingroup dvb_table
151  */
152 extern __gshared const(char)*[] pmt_stream_name;
153 
154 /**
155  * @struct dvb_table_pmt_stream
156  * @brief MPEG-TS PMT stream table
157  * @ingroup dvb_table
158  *
159  * @param type			stream type
160  * @param elementary_pid	elementary pid
161  * @param desc_length		descriptor length
162  * @param zero			zero
163  * @param descriptor		pointer to struct dvb_desc
164  * @param next			pointer to struct dvb_table_pmt_stream
165 
166  *
167  * This structure is used to store the original PMT stream table,
168  * converting the integer fields to the CPU endianness.
169  *
170  * The undocumented parameters are used only internally by the API and/or
171  * are fields that are reserved. They shouldn't be used, as they may change
172  * on future API releases.
173  *
174  * Everything after dvb_table_pmt_stream::descriptor (including it) won't be
175  * bit-mapped to the data parsed from the MPEG TS. So, metadata are added there.
176  */
177 struct dvb_table_pmt_stream
178 {
179     align (1):
180 
181     ubyte type;
182 
183     union
184     {
185         align (1):
186 
187         ushort bitfield;
188 
189         struct
190         {
191             import std.bitmanip : bitfields;
192             align (1):
193 
194             mixin(bitfields!(
195                 ushort, "elementary_pid", 13,
196                 ushort, "reserved", 3));
197         }
198     }
199 
200     union
201     {
202         align (1):
203 
204         ushort bitfield2;
205 
206         struct
207         {
208             import std.bitmanip : bitfields;
209             align (1):
210 
211             mixin(bitfields!(
212                 ushort, "desc_length", 10,
213                 ushort, "zero", 2,
214                 ushort, "reserved2", 4));
215         }
216     }
217 
218     // struct dvb_desc;
219     dvb_desc* descriptor;
220     dvb_table_pmt_stream* next;
221 }
222 
223 /**
224  * @struct dvb_table_pmt
225  * @brief MPEG-TS PMT table
226  * @ingroup dvb_table
227  *
228  * @param header	struct dvb_table_header content
229  * @param pcr_pid	PCR PID
230  * @param desc_length	descriptor length
231  * @param descriptor	pointer to struct dvb_desc
232  * @param stream	pointer to struct dvb_table_pmt_stream
233  *
234  * This structure is used to store the original PMT stream table,
235  * converting the integer fields to the CPU endianness.
236  *
237  * The undocumented parameters are used only internally by the API and/or
238  * are fields that are reserved. They shouldn't be used, as they may change
239  * on future API releases.
240  *
241  * Everything after dvb_table_pmt::descriptor (including it) won't be
242  * bit-mapped to the data parsed from the MPEG TS. So, metadata are added there.
243  */
244 struct dvb_table_pmt
245 {
246     align (1):
247 
248     dvb_table_header header;
249 
250     union
251     {
252         align (1):
253 
254         ushort bitfield;
255 
256         struct
257         {
258             import std.bitmanip : bitfields;
259             align (1):
260 
261             mixin(bitfields!(
262                 ushort, "pcr_pid", 13,
263                 ushort, "reserved2", 3));
264         }
265     }
266 
267     union
268     {
269         align (1):
270 
271         ushort bitfield2;
272 
273         struct
274         {
275             import std.bitmanip : bitfields;
276             align (1):
277 
278             mixin(bitfields!(
279                 ushort, "desc_length", 10,
280                 ushort, "zero3", 2,
281                 ushort, "reserved3", 4));
282         }
283     }
284 
285     dvb_desc* descriptor;
286     dvb_table_pmt_stream* stream;
287 }
288 
289 /** @brief First field at the struct */
290 // enum dvb_pmt_field_first = header;
291 
292 /** @brief First field that are not part of the received data */
293 // enum dvb_pmt_field_last = descriptor;
294 
295 /**
296  * @brief Macro used to find streams on a PMT table
297  * @ingroup dvb_table
298  *
299  * @param _stream	stream to seek
300  * @param _pmt		pointer to struct dvb_table_pmt_stream
301  */
302 
303 // struct dvb_v5_fe_parms;
304 
305 /**
306  * @brief Initializes and parses PMT table
307  * @ingroup dvb_table
308  *
309  * @param parms	struct dvb_v5_fe_parms pointer to the opened device
310  * @param buf buffer containing the PMT raw data
311  * @param buflen length of the buffer
312  * @param table pointer to struct dvb_table_pmt to be allocated and filled
313  *
314  * This function allocates a PMT table and fills the fields inside
315  * the struct. It also makes sure that all fields will follow the CPU
316  * endianness. Due to that, the content of the buffer may change.
317  *
318  * @return On success, it returns the size of the allocated struct.
319  *	   A negative value indicates an error.
320  */
321 ssize_t dvb_table_pmt_init (
322     dvb_v5_fe_parms* parms,
323     const(ubyte)* buf,
324     ssize_t buflen,
325     dvb_table_pmt** table);
326 
327 /**
328  * @brief Frees all data allocated by the PMT table parser
329  * @ingroup dvb_table
330  *
331  * @param table pointer to struct dvb_table_pmt to be freed
332  */
333 void dvb_table_pmt_free (dvb_table_pmt* table);
334 
335 /**
336  * @brief Prints the content of the PAT table
337  * @ingroup dvb_table
338  *
339  * @param parms	struct dvb_v5_fe_parms pointer to the opened device
340  * @param table pointer to struct dvb_table_pmt
341  */
342 void dvb_table_pmt_print (dvb_v5_fe_parms* parms, const(dvb_table_pmt)* table);